// Copyright (c) 2009 Luca Marrocco.
// Modified or written by Luca Marrocco for inclusion with codej.
// Licensed under the Apache License, Version 2.0 (the "License")
package codej.ast;

import java.util.*;

/** @author Luca Marrocco */
public abstract class AbstractNode implements Node, Iterable<Node> {

	private List<Node> children = new LinkedList<Node>();
	
	private Node parent;

	public void addChild(Node child) {
		if (child == null)
			return;
		children.add(child);
		child.parent(this);
	}

	public <T> Iterable<T> filter(Class<? extends T> classT) {
		List<T> list = new LinkedList<T>();
		for (Node node : this) {
			if (classT.isInstance(node))
				list.add((T) node);
		}
		return list;
	}

	public List<Node> getChildren() {
		return children;
	}

	public Iterator<Node> iterator() {
		return getChildren().iterator();
	}

	public Node parent() {
		return parent;
	}

	public void parent(Node parent) {
		this.parent = parent;
	}

}
